package com.ruoyi.api.user;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.api.util.DateUtil;
import com.ruoyi.api.util.GPSUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.user.domain.Health;
import com.ruoyi.user.domain.Location;
import com.ruoyi.user.domain.Machine;
import com.ruoyi.user.domain.Order;
import com.ruoyi.user.domain.Sleep;
import com.ruoyi.user.domain.Sos;
import com.ruoyi.user.domain.Step;
import com.ruoyi.user.service.IHealthService;
import com.ruoyi.user.service.ILocationService;
import com.ruoyi.user.service.IMachineService;
import com.ruoyi.user.service.IOrderService;
import com.ruoyi.user.service.ISleepService;
import com.ruoyi.user.service.ISosService;
import com.ruoyi.user.service.IStepService;
import com.ruoyi.user.service.IUserService;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/api/data")
public class ApiDataController extends BaseController{
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IMachineService machineService;
	
	@Autowired
	private IStepService stepService;
	
	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private ISosService sosService;
	
	@Autowired
	private IHealthService healthService;
	
	@Autowired
	private ILocationService locationService;
	
	@Autowired
	private ISleepService sleepService;
	
	/**
	 * 用户统计数据
	 * @return
	 */
	@PostMapping("/userData")
	@ResponseBody
	@ApiOperation(value="用户统计数据", notes="用户统计数据",produces = "application/json")
	public HashMap<String, Object> userData()
	{		
		LinkedHashMap<String,LinkedHashMap<String,LinkedList<String>>> vo = new LinkedHashMap<String,LinkedHashMap<String,LinkedList<String>>>();
		List<Machine> listMa = machineService.selectMachineList(null);
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		String curDate = format.format(new Date());
		int ageCount1 = 0;
		int ageCount2 = 0;
		int ageCount3 = 0;
		int ageCount4 = 0;
		int ageCount5 = 0;
		int newAdd = 0;
		for(Machine ma:listMa){
			Date date = ma.getBirthday();
			if(date!=null){
				int age = DateUtil.getAge(date);
				if(age<30){
					ageCount1++;
				}else if(age<50){
					ageCount2++;
				}else if(age<65){
					ageCount3++;
				}else if(age<=75){
					ageCount4++;
				}else if(age>75){
					ageCount5++;
				}
			}
			String cc = format.format(ma.getCreateTime()) ;
			if(curDate.equals(cc)){
				newAdd++;
			}
		}
		LinkedHashMap<String,LinkedList<String>> ageMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list = new LinkedList<String>();
		list.add("30以下");
		list.add("30-50");
		list.add("50-65");
		list.add("65-75");
		list.add("75以上");
		ageMap.put("list", list);
		LinkedList<String> data = new LinkedList<String>();
		data.add(String.valueOf(ageCount1));
		data.add(String.valueOf(ageCount2));
		data.add(String.valueOf(ageCount3));
		data.add(String.valueOf(ageCount4));
		data.add(String.valueOf(ageCount5));
		ageMap.put("data", data);
		vo.put("age",ageMap);
		LinkedHashMap<String,LinkedList<String>> stepMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<Step> listStep = stepService.selectStepCount();
		ageCount1 = 0;
		ageCount2 = 0;
		ageCount3 = 0;
		ageCount4 = 0;
		ageCount5 = 0;
		for(Step step:listStep){
			int pcount = step.getPedometer();
			if(pcount<3000){
				ageCount1++;
			}else if(pcount<5000){
				ageCount2++;
			}else if(pcount<7000){
				ageCount3++;
			}else if(pcount<=10000){
				ageCount4++;
			}else if(pcount>10000){
				ageCount5++;
			}
		}
		LinkedList<String> list1 = new LinkedList<String>();
		list1.add("3000以下");
		list1.add("3000-5000");
		list1.add("5000-7000");
		list1.add("7000-10000");
		list1.add("10000以上");
		stepMap.put("list", list1);
		LinkedList<String> data1 = new LinkedList<String>();
		data1.add(String.valueOf(ageCount1));
		data1.add(String.valueOf(ageCount2));
		data1.add(String.valueOf(ageCount3));
		data1.add(String.valueOf(ageCount4));
		data1.add(String.valueOf(ageCount5));
		stepMap.put("data", data1);
		vo.put("step", stepMap);
		LinkedHashMap<String,LinkedList<String>> sexMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list2 = new LinkedList<String>();
		int sexCount1 = 0;//男
		int sexCount2 = 0;//女
		for(Machine ma:listMa){
			int sex = ma.getSex();
			if(sex==0){
				sexCount1++;
			}else{
				sexCount2++;
			}
		}
		list2.add("男");
		list2.add("女");
		sexMap.put("list", list2);
		LinkedList<String> data2 = new LinkedList<String>();
		data2.add(String.valueOf(sexCount1));
		data2.add(String.valueOf(sexCount2));
		sexMap.put("data", data2);
		vo.put("sex", sexMap);
		LinkedHashMap<String,LinkedList<String>> vipMap = new LinkedHashMap<String,LinkedList<String>>();
		Order param = new Order();
    	param.setStatus(2);
    	List<Order> listOrder = orderService.selectOrderList(param);//。。。
    	Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    	int newSAdd = 0;
    	for(Order order:listOrder){
    		Date date = order.getCreateTime();
    		int timeMonth = DateUtil.getTimeMonthInt(date);
    		if(map.containsKey(timeMonth)){
    			map.put(timeMonth, map.get(timeMonth)+1);
    		}else{
    			map.put(timeMonth, 1);
    		}
    		String cc = format.format(order.getCreateTime()) ;
			if(curDate.equals(cc)){
				newSAdd++;
			}
    	}
    	int curMonth = DateUtil.getCurrentMonthInt();
    	LinkedList<Integer> vList = new LinkedList<Integer>();
    	LinkedList<String> list3 = new LinkedList<String>();
    	LinkedList<String> data3 = new LinkedList<String>();
    	for(int i=1;i<=curMonth;i++){
    		vList.add(i);
    		list3.add(i+"月");
    		data3.add(String.valueOf(map.get(i)));
    	}
    	vipMap.put("list", list3);
		vipMap.put("data", data3);
		vo.put("vip", vipMap);
		LinkedHashMap<String,LinkedList<String>> sosMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list4 = new LinkedList<String>();
		LinkedList<String> data4 = new LinkedList<String>();
		List<Sos> sosList = sosService.selectSosList(null);
		Map<Integer,Integer> map1 = new HashMap<Integer,Integer>();
		Date nowDate = new Date();
		int sosNow = 0;
		for(Sos sos:sosList){
			Date date = sos.getCreateTime();
    		int timeMonth = DateUtil.getTimeMonthInt(date);
    		if(map1.containsKey(timeMonth)){
    			map1.put(timeMonth, map1.get(timeMonth)+1);
    		}else{
    			map1.put(timeMonth, 1);
    		}
    		boolean b = DateUtil.dateEquals(nowDate, date);
    		if(b){
    			sosNow++;
    		}
		}
		for(int i=1;i<=curMonth;i++){
    		vList.add(i);
    		list4.add(i+"月");
    		data4.add(String.valueOf(String.valueOf(map1.get(i))));
    	}
		sosMap.put("list", list4);
		sosMap.put("data", data4);
		
		LinkedHashMap<String,LinkedList<String>> sosCountMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> countSosList4 = new LinkedList<String>();
		LinkedList<String> countSosData4 = new LinkedList<String>();
		countSosList4.add("今日呼叫");
		countSosList4.add("累积呼叫");
		sosCountMap.put("list", countSosList4);
		countSosData4.add(String.valueOf(sosNow));
		countSosData4.add(String.valueOf(sosList.size()));
		sosCountMap.put("data", countSosData4);
		vo.put("sos", sosMap);
		vo.put("sosUser", sosCountMap);
		
		LinkedHashMap<String,LinkedList<String>> userCountMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list5 = new LinkedList<String>();
		list5.add("用户总量");
		list5.add("今日新增");
		list5.add("服务总量");
		list5.add("服务新增");
		userCountMap.put("list", list5);
		LinkedList<String> data5 = new LinkedList<String>();
		data5.add(String.valueOf(listMa.size()));
		data5.add(String.valueOf(newAdd));
		data5.add(String.valueOf(listOrder.size()));
		data5.add(String.valueOf(newSAdd));
		userCountMap.put("data", data5);
		vo.put("userCount", userCountMap);
		return successApp(vo);
	}
	
	/**
	 * 用户健康状态统计数据
	 * @return
	 */
	@PostMapping("/healthStatusData")
	@ResponseBody
	@ApiOperation(value="用户健康状态统计数据", notes="用户健康统计数据",produces = "application/json")
	public HashMap<String, Object> healthStatusData()
	{		
		LinkedHashMap<String,LinkedList<String>> vo = new LinkedHashMap<String,LinkedList<String>>();
		List<Machine> listMa = machineService.selectMachineList(null);
		int h1 = 0;
		int h2 = 0;
		int h3 = 0;
		for(Machine ma:listMa){
			List<Health> list = healthService.selectHealthNewList(ma.getCode());
			boolean flg = true;
			if(list!=null&&list.size()>0){
				Health health = list.get(0);
				if(health.getSdp().intValue()>150||health.getDbp().intValue()<60){//心率低于60，高于110，就为异常
					flg = false;
				}
				if(health.getHeartRate().intValue()<60||health.getHeartRate().intValue()>110){//血压  舒张压低于60  收缩压高于150就为异常
					flg = false;
				}
			}
			if(flg){
				h1++;
			}else{
				h2++;
			}
		}
		LinkedList<String> list2 = new LinkedList<String>();
		list2.add("健康");
		list2.add("亚健康");
		list2.add("疾病");
		vo.put("list", list2);
		LinkedList<String> data2 = new LinkedList<String>();
		data2.add(String.valueOf(h1));
		data2.add(String.valueOf(h2));
		data2.add("0");
		vo.put("data", data2);
		
		return successApp(vo);
	}
	
	/**
	 * 用户健康统计数据
	 * @return
	 */
	@PostMapping("/healthData")
	@ResponseBody
	@ApiOperation(value="用户健康统计数据", notes="用户健康统计数据",produces = "application/json")
	public HashMap<String, Object> healthData()
	{		
		LinkedHashMap<String,LinkedHashMap<String,LinkedList<String>>> vo = new LinkedHashMap<String,LinkedHashMap<String,LinkedList<String>>>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<Health> listRate = healthService.selectHealthAnalysisList(sdf.format(new Date()));
		List<Health> list = healthService.selectHealthAnalysisListOne(sdf.format(new Date()));
		LinkedHashMap<String,LinkedList<String>> heartBeatMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list2 = new LinkedList<String>();
		list2.add("心率正常");
		list2.add("心率过快");
		list2.add("心率过慢");
		list2.add("心率不齐");
		heartBeatMap.put("list", list2);
		LinkedList<String> data2 = new LinkedList<String>();
		int b = 0;//心率偏快
		int b1 = 0;//心率偏慢
		int b2 = 0;//正常
		int b3 = 0;//心率过快
		/*心率过快> 160 >心率偏快 >110>正常>55>心率偏慢*/
		if(listRate!=null&&listRate.size()>0){
			for(Health hl:listRate){
				if(hl.getHeartRate().intValue()>160){//心率过快
					b3++;
				}else if(hl.getHeartRate().intValue()>110){//心率偏快
					b++;
				}else if(hl.getHeartRate().intValue()>55){//正常
					b2++;
				}else{//心率偏慢
					b1++;
				}
			}
		}
		data2.add(String.valueOf(b2));
		data2.add(String.valueOf(b));
		data2.add(String.valueOf(b1));
		data2.add(String.valueOf(b3));
		heartBeatMap.put("data", data2);
		vo.put("heartBeat", heartBeatMap);
		LinkedHashMap<String,LinkedList<String>> bloodMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list3 = new LinkedList<String>();
		list3.add("正常");
		list3.add("1级高血压");
		list3.add("2级高血压");
		list3.add("3级高血压");
		bloodMap.put("list", list3);
		LinkedList<String> data3 = new LinkedList<String>();
		int v = 0;//1级高血压(轻度)
		int v1 = 0;//2级高血压(中度)
		int v2 = 0;//3级高血压(重度)
		int v3 = 0;//正常
		if(list!=null&&list.size()>0){
			for(Health hl:list){
				if((hl.getSdp().intValue()>=140&&hl.getSdp().intValue()<=159)&&(hl.getDbp().intValue()>=90&&hl.getDbp().intValue()<=99)){//1级高血压(轻度)
					v++;
				}else if((hl.getSdp().intValue()>=160&&hl.getSdp().intValue()<=179)&&(hl.getDbp().intValue()>=100&&hl.getDbp().intValue()<=109)){//2级高血压(中度)
					v1++;
				}else if((hl.getSdp().intValue()>=180)&&(hl.getDbp().intValue()>=110)){//3级高血压(重度)
					v2++;
				}else{//正常
					v3++;
				}
			}
		}
		data3.add(String.valueOf(v3));
		data3.add(String.valueOf(v));
		data3.add(String.valueOf(v1));
		data3.add(String.valueOf(v2));
		bloodMap.put("data", data3);
		vo.put("blood", bloodMap);
		LinkedHashMap<String,LinkedList<String>> sexMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list4 = new LinkedList<String>();
		list4.add("男");
		list4.add("女");
		sexMap.put("list", list4);
		LinkedList<String> data4 = new LinkedList<String>();
		data4.add("50");
		data4.add("45");
		sexMap.put("data", data4);
		vo.put("sex", sexMap);
		LinkedHashMap<String,LinkedList<String>> xueyangMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list5 = new LinkedList<String>();
		list5.add("正常");
		list5.add("异常");
		xueyangMap.put("list", list5);
		LinkedList<String> data5 = new LinkedList<String>();
		data5.add("0");
		data5.add("0");
		xueyangMap.put("data", data5);
		vo.put("xueyang", xueyangMap);
		LinkedHashMap<String,LinkedList<String>> xindianMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list6 = new LinkedList<String>();
		list6.add("正常");
		list6.add("异常");
		xindianMap.put("list", list6);
		LinkedList<String> data6 = new LinkedList<String>();
		data6.add("0");
		data6.add("0");
		xindianMap.put("data", data6);
		vo.put("xindian", xindianMap);
		
		LinkedHashMap<String,LinkedList<String>> sleepMap = new LinkedHashMap<String,LinkedList<String>>();
		LinkedList<String> list7 = new LinkedList<String>();
		LinkedList<String> data7 = new LinkedList<String>();
		Sleep param = new Sleep();
		Date now = new Date();
		try {
			param.setSleepDate(sdf.parse(sdf.format(now)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int eh = 8*60;
		int sh = 6*60;
		int fh = 4*60;
		int sCount0 = 0;
		int sCount1 = 0;
		int sCount2 = 0;
		int sCount3 = 0;
		List<Sleep> listSlp = sleepService.selectSleepList(param);
		for(Sleep slp:listSlp){
			int totalCount = 0;
			if(slp.getTotalCount()!=null&&slp.getTotalCount()>0){
				totalCount = slp.getTotalCount();
			}else{
				totalCount = slp.getDeepCount()+slp.getEasyCount();
			}
			if(totalCount<fh){
				sCount3++;
			}else if(totalCount>=fh&&totalCount<sh){
				sCount2++;
			}else if(totalCount>=sh&&totalCount<eh){
				sCount1++;
			}else{
				sCount0++;
			}
		}
		list7.add("8h以上");
		list7.add("6-8h");
		list7.add("4-6h");
		list7.add("4h以下");
		data7.add(String.valueOf(sCount0));
		data7.add(String.valueOf(sCount1));
		data7.add(String.valueOf(sCount2));
		data7.add(String.valueOf(sCount3));
		sleepMap.put("list", list7);
		sleepMap.put("data", data7);
		vo.put("sleep", sleepMap);
		
		return successApp(vo);
	}
	
	/**
	 * 用户定位统计数据
	 * @return
	 */
	@PostMapping("/locationData")
	@ResponseBody
	@ApiOperation(value="用户定位统计数据", notes="用户定位统计数据",produces = "application/json")
	public HashMap<String, Object> locationData()
	{		
		LinkedHashMap<String,LinkedHashMap<String,LinkedList<String>>> vo = new LinkedHashMap<String,LinkedHashMap<String,LinkedList<String>>>();
		LinkedHashMap<String,LinkedList<String>> locationMap = new LinkedHashMap<String,LinkedList<String>>();
		List<Location> listLocation = locationService.selectLocationCount();
		LinkedList<String> list6 = new LinkedList<String>();
		LinkedList<String> data6 = new LinkedList<String>();
		for(Location lo:listLocation){
			String address = GPSUtil.getAmapByLngAndLat(lo.getLon(), lo.getLat());
			list6.add(address+"[设备编号:"+lo.getImei()+"]");
			data6.add(lo.getLon()+","+lo.getLat());
		}
		/*list6.add("北京市");
		list6.add("天津市");
		list6.add("河北省");*/
		locationMap.put("list", list6);
		/*data6.add("116.407394,39.904211");
		data6.add("117.200983,39.084158");
		data6.add("114.530235,38.037433");*/
		locationMap.put("data", data6);
		vo.put("location", locationMap);
		return successApp(vo);
	}

}
